/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package marvell.JobSeeker;

import Connection.MyConnect;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Quang
 */
public class JobSeeker_Manager extends javax.swing.JPanel {
    public MyConnect iconnect;
    public ResultSet rs;
    public CallableStatement stm;
    
    
    public AddNewJobSeeker anjs;
    public UpdateJobSeeker upjs;
    public ViewStaticJobSeeker vjs;
    
    public DefaultTableModel tbl = new DefaultTableModel();
    public DefaultListModel Skills = new DefaultListModel();
    public DefaultListModel Select = new DefaultListModel();
    public int ik = 0;
    public boolean a = true;
    public JobSeeker_Manager() {
        initComponents();
        try
        {
            iconnect = new MyConnect();
            tbl = (DefaultTableModel)jtblJS.getModel();
            
        }catch(Exception ex){ex.printStackTrace();}    
    }
    
    public void LoadDataToTable()
    {
        try
        {
            String sql = "{call getAlltoTable}";
            stm = MyConnect.iconnect.prepareCall(sql);
            rs = stm.executeQuery();
            while(rs.next())
            {
                tbl.addRow(new String[] {rs.getString(1),rs.getString(2),rs.getString(3),
                    rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8)});
            }    
        }catch(Exception ex){ex.printStackTrace();}    
    }  
    public void deleteJobSeeker()
    {
        try
        {
            String sql = "{call deleteJS(?)}";
            stm = MyConnect.iconnect.prepareCall(sql);
            stm.setInt(1,ik);
            if(!stm.execute()){JOptionPane.showMessageDialog(this,"Delete successful.");}
            else{JOptionPane.showMessageDialog(this,"Delete fail.");}
        }
        catch(Exception ex){ex.printStackTrace();}
    }        
    
    public boolean ClickTable()
    {
        ik = 0;
        int[] k = jtblJS.getSelectedRows();
        if(k.length == 0){JOptionPane.showMessageDialog(this,"No row is selecting."); return false;}
        if(k.length > 2){JOptionPane.showMessageDialog(this,"Just can select one row.");return false;}
        else{ik = Integer.parseInt(String.valueOf(jtblJS.getValueAt(jtblJS.getSelectedRow(),0)));return true;}
        
    }   

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////       
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtblJS = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jbtnViewJS = new javax.swing.JButton();
        jbtnAddJS = new javax.swing.JButton();
        jbtnUpdateJS = new javax.swing.JButton();
        jbtnDeleteJS = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jbtnRefresh = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "JobSeeker Manager", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 18), new java.awt.Color(255, 0, 0))); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(800, 600));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "All of JobSeeker", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Palatino Linotype", 1, 18), new java.awt.Color(0, 51, 51))); // NOI18N

        jtblJS.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Full name", "Gender", "Phone", "Location", "Position", "Job", "Status"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtblJS.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtblJSMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtblJS);
        jtblJS.getColumnModel().getColumn(0).setPreferredWidth(25);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 756, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Button Control", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(0, 51, 51))); // NOI18N

        jbtnViewJS.setText("View");
        jbtnViewJS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnViewJSActionPerformed(evt);
            }
        });

        jbtnAddJS.setText("Add");
        jbtnAddJS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAddJSActionPerformed(evt);
            }
        });

        jbtnUpdateJS.setText("Update");
        jbtnUpdateJS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnUpdateJSActionPerformed(evt);
            }
        });

        jbtnDeleteJS.setText("Delete");
        jbtnDeleteJS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnDeleteJSActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jbtnViewJS)
                .addGap(29, 29, 29)
                .addComponent(jbtnAddJS)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                .addComponent(jbtnUpdateJS)
                .addGap(31, 31, 31)
                .addComponent(jbtnDeleteJS)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnViewJS)
                    .addComponent(jbtnAddJS)
                    .addComponent(jbtnUpdateJS)
                    .addComponent(jbtnDeleteJS))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Report", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(0, 51, 51))); // NOI18N

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 401, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 34, Short.MAX_VALUE)
        );

        jbtnRefresh.setText("Refresh");
        jbtnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnRefreshActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbtnRefresh)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jbtnRefresh)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(137, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnAddJSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAddJSActionPerformed
        // TODO add your handling code here:
        anjs = new AddNewJobSeeker();
        anjs.setVisible(true);
    }//GEN-LAST:event_jbtnAddJSActionPerformed

    private void jbtnUpdateJSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnUpdateJSActionPerformed
        // TODO add your handling code here:
        if(ClickTable())
        {upjs = new UpdateJobSeeker();
        UpdateJobSeeker.id = this.ik;
         upjs.setVisible(true);        
        }
    }//GEN-LAST:event_jbtnUpdateJSActionPerformed
    
    private void jtblJSMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtblJSMouseClicked
        // TODO add your handling code here:\
        try {
            ClickTable();
            ViewStaticJobSeeker.id = this.ik;/////////////  VREY IMPORTANT CODE /// USE THIS CODE FOR EACH JFRAME
            UpdateJobSeeker.id = this.ik;/////////////  VREY IMPORTANT CODE /// USE THIS CODE FOR EACH JFRAME
        } catch (Exception ex){ex.printStackTrace();}
    }//GEN-LAST:event_jtblJSMouseClicked

    private void jbtnDeleteJSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnDeleteJSActionPerformed
        // TODO add your handling code here:
        if(ClickTable()){deleteJobSeeker();}
    }//GEN-LAST:event_jbtnDeleteJSActionPerformed

    private void jbtnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnRefreshActionPerformed
        // TODO add your handling code here:
        LoadDataToTable();
    }//GEN-LAST:event_jbtnRefreshActionPerformed

    private void jbtnViewJSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnViewJSActionPerformed
        // TODO add your handling code here:
         if(ClickTable())
        {vjs = new ViewStaticJobSeeker();
         ViewStaticJobSeeker.id = this.ik;
         vjs.setVisible(true);         
        }
    }//GEN-LAST:event_jbtnViewJSActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnAddJS;
    private javax.swing.JButton jbtnDeleteJS;
    private javax.swing.JButton jbtnRefresh;
    private javax.swing.JButton jbtnUpdateJS;
    private javax.swing.JButton jbtnViewJS;
    private javax.swing.JTable jtblJS;
    // End of variables declaration//GEN-END:variables
}
